﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using yaTDL.Core;
using yaTDL.Core.Toodledo;
using yaTDL.Extensions;

namespace yaTDL
{
    /// <summary>
    /// Interaction logic for ToodledoTestWindow.xaml
    /// </summary>
    public partial class ToodledoTestWindow : Window
    {
        public ToodledoTestWindow()
        {
            InitializeComponent();
        }

        ToodledoClientV1 client = new ToodledoClientV1();

        private void button_GetKey_Click(object sender, RoutedEventArgs e)
        {
            textBox_result_NewContent();

            textBox_key.Text = client.GetKey(textBox_UserId.Text, textBox_Password.Text).Key;
            textBox_result.Text = client.GetKey(textBox_UserId.Text, textBox_Password.Text).ToString();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            textBox_result_NewContent();

            Collection<Project> folders = client.GetFolders(textBox_key.Text);

            if (folders == null) return;

            foreach (Project p in folders)
            {
                textBox_result.Text += p.ToString() + "\n";
            }


        }

        private void button_getUserId_Click(object sender, RoutedEventArgs e)
        {
            textBox_result_NewContent();

            textBox_result.Text = client.GetUserId(textBox_getUserId_email.Text, textBox_getUserId_password.Text);
        }

        private void button_addFolder_Click(object sender, RoutedEventArgs e)
        {
            textBox_result_NewContent();

            Project p = new Project() { Name = textBox_addFolder_Title.Text };
            if (client.AddFolder(textBox_key.Text, p))
            { textBox_result.Text = p.ToString(); }
            else
            { textBox_result.Text = "Error"; }
        }

        private void button_deleteFolder_Click(object sender, RoutedEventArgs e)
        {
            textBox_result_NewContent();
            bool wasSuccessful = client.DeleteFolder(textBox_key.Text, textBox_deleteFolder_Id.Text);
            textBox_result.Text = "was successful: " + wasSuccessful.ToString();
        }

        private void button_getServerInfo_Click(object sender, RoutedEventArgs e)
        {
            textBox_result_NewContent();

            textBox_result.Text = client.GetServerInfo(textBox_key.Text).ToString();
        }

        private void button_getAccountInfo_Click(object sender, RoutedEventArgs e)
        {
            textBox_result_NewContent();
            textBox_result.Text = client.GetAccountInfo(textBox_key.Text).ToString();
        }

        private void button_getTasks_Click(object sender, RoutedEventArgs e)
        {
            textBox_result_NewContent();
            List<Parameter> parameters = new List<Parameter>();

            if (datePicker_compafter.SelectedDate != null) 
            {
                parameters.Add(new Parameter() { Name = "compafter", Value = datePicker_compafter.SelectedDate });
            }
            if (datePicker_compbefore.SelectedDate != null)
            {
                parameters.Add(new Parameter() { Name = "compbefore", Value = datePicker_compbefore.SelectedDate });
            }
            if (datePicker_modafter.SelectedDate != null)
            {
                parameters.Add(new Parameter() { Name = "modafter", Value = datePicker_modafter.SelectedDate });
            }
            if (datePicker_modbefore.SelectedDate != null)
            {
                parameters.Add(new Parameter() { Name = "modbefore", Value = datePicker_modbefore.SelectedDate });
            }

            if (!String.IsNullOrEmpty(textBox_folder.Text))
            {
                parameters.Add(new Parameter() { Name = "folder", Value = textBox_folder.Text });
            }


            parameters.Add(new Parameter() { Name = "notcomp", Value = (bool) checkBox_notcomp.IsChecked });

            Collection<Task> tasks = client.GetTasks(textBox_key.Text, parameters);

            foreach (Task item in tasks)
            {
                textBox_result.Text += item.ToString() + "\n";
            }
        }


        private void button_addTask_Click(object sender, RoutedEventArgs e)
        {
            Task t = new Task();
            t.Content = textBox_addTask_title.Text;
            t.Priority = (int)slider_addTask_priprity.Value;
            if (!String.IsNullOrEmpty(textBox_addTask_folder.Text)) { t.ProjectId = textBox_addTask_folder.Text; }
            if (datePicker_addTask_duedate.SelectedDate != null) { t.DueDate = (DateTime)datePicker_addTask_duedate.SelectedDate; }

            textBox_result_NewContent();
            bool wasSuccessful = client.AddTask(textBox_key.Text, t);
            textBox_result.Text = "was successful: " + wasSuccessful.ToString();
            if (wasSuccessful)
            {
                textBox_result.Text += "\n" + t.ToString();
            }
        }


        private void button_deleteTask_Click(object sender, RoutedEventArgs e)
        {
            textBox_result_NewContent();
            bool wasSuccessful = client.DeleteTask(textBox_key.Text, textBox_deleteTask_id.Text);
            textBox_result.Text = "was successful: " + wasSuccessful.ToString();
        }

        private void button_getDeleted_Click(object sender, RoutedEventArgs e)
        {
            textBox_result_NewContent();

            if (datePicker_getDeleted_after.SelectedDate == null) { datePicker_getDeleted_after.SelectedDate = new DateTime(); }

            Collection<string> ids = client.GetDeletedTasks(textBox_key.Text, (DateTime)datePicker_getDeleted_after.SelectedDate);

            foreach (string item in ids)
            {
                textBox_result.Text += item + "\n";
            }
        }

        private void button_DateTimeUnixConversion_toTimestamp_Click(object sender, RoutedEventArgs e)
        {
            textBox_result_NewContent();
            if (datePicker_DateTimeUnixConversion.SelectedDate != null)
            {
                textBox_result.Text = ((DateTime)datePicker_DateTimeUnixConversion.SelectedDate).ToString() + "\n" + ((DateTime)datePicker_DateTimeUnixConversion.SelectedDate).ToUnixTime().ToString();
            }
        }

        private void button_DateTimeUnixConversion_toDateTime_Click(object sender, RoutedEventArgs e)
        {
            textBox_result_NewContent();
            if (!String.IsNullOrEmpty(textBox_DateTimeUnixConversion_timestamp.Text))
            {
                double d = double.Parse(textBox_DateTimeUnixConversion_timestamp.Text);
                textBox_result.Text = d.ToString() + "\n" + d.ToDateTime().ToString();
            }
    
        }

        private void button_clearResults_Click(object sender, RoutedEventArgs e)
        {
            textBox_result.Clear();
        }


        private void textBox_result_NewContent()
        {
            if (!String.IsNullOrEmpty(textBox_result.Text))
            {
                textBox_result.Text += "\n\n";
            }
        }
        
        
    }
}
